<!DOCTYPE html>
<html lang="zh-CN">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>SSE Token验证测试</title>
    <style>
        body {
            font-family: Arial, sans-serif;
            max-width: 800px;
            margin: 0 auto;
            padding: 20px;
            background-color: #f5f5f5;
        }
        .container {
            background: white;
            padding: 20px;
            border-radius: 8px;
            box-shadow: 0 2px 10px rgba(0,0,0,0.1);
        }
        .form-group {
            margin-bottom: 15px;
        }
        label {
            display: block;
            margin-bottom: 5px;
            font-weight: bold;
        }
        input, textarea {
            width: 100%;
            padding: 8px;
            border: 1px solid #ddd;
            border-radius: 4px;
            box-sizing: border-box;
        }
        button {
            background: #007bff;
            color: white;
            padding: 10px 20px;
            border: none;
            border-radius: 4px;
            cursor: pointer;
            margin-right: 10px;
        }
        button:hover {
            background: #0056b3;
        }
        .log {
            background: #f8f9fa;
            border: 1px solid #dee2e6;
            border-radius: 4px;
            padding: 10px;
            height: 300px;
            overflow-y: auto;
            font-family: monospace;
            font-size: 12px;
        }
        .status {
            padding: 10px;
            margin: 10px 0;
            border-radius: 4px;
        }
        .status.success {
            background: #d4edda;
            color: #155724;
            border: 1px solid #c3e6cb;
        }
        .status.error {
            background: #f8d7da;
            color: #721c24;
            border: 1px solid #f5c6cb;
        }
        .status.info {
            background: #d1ecf1;
            color: #0c5460;
            border: 1px solid #bee5eb;
        }
    </style>
</head>
<body>
    <div class="container">
        <h1>SSE Token验证测试</h1>
        <p>测试不同的token验证场景</p>

        <div class="form-group">
            <label for="baseUrl">服务端地址:</label>
            <input type="text" id="baseUrl" value="http://localhost:8080">
        </div>

        <div class="form-group">
            <label for="token">Token:</label>
            <input type="text" id="token" placeholder="请输入有效的token">
        </div>

        <div class="form-group">
            <label for="userId">用户ID (可选):</label>
            <input type="number" id="userId" placeholder="用户ID，仅用于测试">
        </div>

        <div class="form-group">
            <button onclick="testValidToken()">测试有效Token</button>
            <button onclick="testInvalidToken()">测试无效Token</button>
            <button onclick="testNoToken()">测试无Token</button>
            <button onclick="testWithUserId()">测试Token+UserId</button>
            <button onclick="clearLog()">清空日志</button>
        </div>

        <div id="status"></div>

        <h3>测试日志:</h3>
        <div id="log" class="log"></div>
    </div>

    <script>
        function log(message, type = 'info') {
            const logDiv = document.getElementById('log');
            const timestamp = new Date().toLocaleTimeString();
            const logEntry = document.createElement('div');
            logEntry.innerHTML = `<span style="color: #666;">[${timestamp}]</span> ${message}`;
            logDiv.appendChild(logEntry);
            logDiv.scrollTop = logDiv.scrollHeight;
        }

        function showStatus(message, type) {
            const statusDiv = document.getElementById('status');
            statusDiv.innerHTML = `<div class="status ${type}">${message}</div>`;
        }

        function clearLog() {
            document.getElementById('log').innerHTML = '';
            document.getElementById('status').innerHTML = '';
        }

        async function testConnection(token, userId = null) {
            const baseUrl = document.getElementById('baseUrl').value;
            let url = `${baseUrl}/api/client/sse/connect?token=${encodeURIComponent(token)}`;
            
            if (userId) {
                url += `&userId=${userId}`;
            }

            log(`尝试连接: ${url}`, 'info');

            return new Promise((resolve, reject) => {
                const eventSource = new EventSource(url);
                
                eventSource.onopen = (event) => {
                    log('连接已建立', 'success');
                    eventSource.close();
                    resolve('连接成功');
                };

                eventSource.onerror = (event) => {
                    log('连接失败', 'error');
                    eventSource.close();
                    reject('连接失败');
                };

                // 设置超时
                setTimeout(() => {
                    eventSource.close();
                    reject('连接超时');
                }, 5000);
            });
        }

        async function testValidToken() {
            const token = document.getElementById('token').value.trim();
            if (!token) {
                showStatus('请输入有效的token', 'error');
                return;
            }

            try {
                log('=== 测试有效Token ===', 'info');
                const result = await testConnection(token);
                showStatus(result, 'success');
                log('测试结果: ' + result, 'success');
            } catch (error) {
                showStatus('测试失败: ' + error, 'error');
                log('测试结果: ' + error, 'error');
            }
        }

        async function testInvalidToken() {
            log('=== 测试无效Token ===', 'info');
            try {
                const result = await testConnection('invalid-token-123');
                showStatus(result, 'success');
                log('测试结果: ' + result, 'success');
            } catch (error) {
                showStatus('测试失败: ' + error, 'error');
                log('测试结果: ' + error, 'error');
            }
        }

        async function testNoToken() {
            log('=== 测试无Token ===', 'info');
            try {
                const result = await testConnection('');
                showStatus(result, 'success');
                log('测试结果: ' + result, 'success');
            } catch (error) {
                showStatus('测试失败: ' + error, 'error');
                log('测试结果: ' + error, 'error');
            }
        }

        async function testWithUserId() {
            const token = document.getElementById('token').value.trim();
            const userId = document.getElementById('userId').value;
            
            if (!token) {
                showStatus('请输入token', 'error');
                return;
            }

            log('=== 测试Token+UserId ===', 'info');
            try {
                const result = await testConnection(token, userId);
                showStatus(result, 'success');
                log('测试结果: ' + result, 'success');
            } catch (error) {
                showStatus('测试失败: ' + error, 'error');
                log('测试结果: ' + error, 'error');
            }
        }

        // 页面加载时的初始化
        window.onload = function() {
            log('SSE Token验证测试页面已加载', 'info');
            log('请选择测试场景进行验证', 'info');
        };
    </script>
</body>
</html> 